<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/3
 * Time: 16:40
 */
namespace app\online\controller;

use think\Db;
use think\Request;
use think\Session;

class Shopping extends Common
{
	//判断是否登录
	public function _initialize()
	{
		parent::_initialize();
	}
	
	/**
	 * 购物车页面
	 * @param string get.bid 商家id
	*/
	public function index()
	{
		//总价
		$total = 0;
		$bid = input('get.bid');
		$result = Db::table('shopping')
				->field('product.pname,product.bid,product.id,product.purchaseprice,
						shopping.number,shopping.id as shopid')
				->join('product','product.id = shopping.pid','LEFT')
				->where('shopping.aid',Session::get('adminuser.id'))
				->where('shopping.status',1)
				->where('shopping.bid',$bid)
				->select();
		foreach($result as $val){
			//计算总价
			$total = $total+$val['purchaseprice']*$val['number'];
		}
		return view('index',compact('result','total','bid'));
	}
	/**
	 * 改变数量
	 * @param string post.id 购物车id
	 * @param string post.bid 商家id
	 * @param string post.number 购买商品的数量
	*/
	public function change()
	{
		if(request()->isAjax()){
			try{
				$total = 0;
				$bid = input('post.bid');
				$id = input('post.id');
				$number = input('post.number');
				$result = Db::table('shopping')->where('id',$id)->update(['number'=>$number]);
				if($result){
					$info = Db::table('shopping')
						->field('product.purchaseprice,shopping.number')
						->join('product','product.id = shopping.pid','LEFT')
						->where('shopping.aid',Session::get('adminuser.id'))
						->where('shopping.status',1)
						->where('shopping.bid',$bid)
						->select();
					foreach($info as $value){
						$total = $total + $value['number'] * $value['purchaseprice'];
					}
					$success = ['total'=>$total];
					return json($success);
				}else{
					throw new \PDOException('修改失败');
				}
			}catch (\PDOException $e){
				$error = ['error '=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 删除
	 * @param string post.id 购物车id
	 * @param string post.bid 商家id
	*/
	public function del()
	{
		if(request()->isAjax()){
			try{
				//计算总价
				$total = 0;
				$id = input('post.id');
				$bid = input('post.bid');
				$result = Db::table('shopping')->where('id',$id)->update(['status'=>0]);
				if(!$result){
					throw new \PDOException('删除失败');
				}
				$info = Db::table('shopping')
					->field('product.purchaseprice,shopping.number')
					->join('product','product.id = shopping.pid','LEFT')
					->where('shopping.aid',Session::get('adminuser.id'))
					->where('shopping.bid',$bid)
					->where('shopping.status',1)
					->select();
				foreach($info as $value){
					$total = $total + $value['number'] * $value['purchaseprice'];
				}
				$success = ['total'=>$total];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 上传支付凭证
	 * @param array request()->file('prove') 图片
	*/
	public function prove()
	{
		if(request()->isAjax()){
			try{
				if(request()->file('prove')){
					$prove = request()->file('prove');
					$provePath = ROOT_PATH.'public'.DS.'online'.DS.Session::get('adminuser.id').DS.'prove';
					$info = $prove->validate(['size'=>500000,'ext'=>'jpg,png,gif,jpeg'])->move($provePath);
					if($info){
						$path = 'online'.DS.Session::get('adminuser.id').DS.'prove'.DS.$info->getSaveName();
						$success = ['prove'=>$path];
						return json($success);
					}else{
						throw new \PDOException($prove->getError());
					}
				}else{
					throw new \PDOException('请添加支付凭证');
				}
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 添加订单
	 * @param string $info['image'] 支付凭证
	 * @param string $info['linkman'] 联系人
	 * @param string $info['telephone'] 联系电话
	 * @param string $info['total'] 总价
	 * @param string $info['time'] 购买时间
	 * @param string $info['ordernumber'] 订单号
	 * @param string $info['address'] 邮寄具体地址
	 * @param array $info['shopid'] 购物车id
	 * @param array $info['pid'][$i] 商品id
	 * @param array $info['pname'][$i] 商家名
	 * @param array $info['price'][$i] 代价
	 * @param array $info['number'][$i] 数量
	 * @param array $district[0]; 省
	 * @param array $district[1]; 市
	 * @param array $district[2]; 县
	 * @param array $district[3]; 镇
	*/
	
	public function insert( Request $request)
	{
		if(request()->isAjax()){
			try{
				//开始事务
				Db::startTrans();
				$info = $request->param();
				$input['prove'] = $info['image'];
				$input['linkman'] = $info['linkman'];
				$input['telephone'] = $info['telephone'];
				$input['total'] = $info['total'];
				$input['ordernumber'] = $input['time'] = time();
				$input['address'] = $info['address'];
				$shopid = $info['shopid'];
				$district = Db::table('district')->whereIn('id',$info['province'])->column('name');
				for($i=0;$i<count($shopid);$i++){
					$input['shopid'] = $shopid[$i];
					$input['pid'] = $info['pid'][$i];
					$input['bid'] = $info['bid'][$i];
					$input['pname'] = $info['pname'][$i];
					$input['price'] = $info['price'][$i];
					$input['number'] = $info['number'][$i];
					$input['province'] = $district[0];
					$input['city'] = $district[1];
					$input['county'] = $district[2];
					//判断是否为4级联动
					if(count($district) == 4){
						$input['town'] = $district[3];
					}
					$id = Db::table('mail')->insertGetId($input);
					if($id>0){
						Db::table('shopping')->where('id',$input['shopid'])->update(['status'=>2]);
					}
				}
				Db::commit();
				$success = ['success'=>'操作成功'];
				return json($success);
			}catch (\PDOException $e){
				//事务回滚
				Db::rollback();
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
}